clear all
use "datasets/MRA_waste_sites"

/* IMPORTANT NOTE: 
The commands 

metareg, wsse(SE)
vwls, sd(SE)
reg [aweight=1/SE²]
reg with manually weighted regressors 

give the exact same results (coefficients and t-values). With no statistical guidance (Ringqvist 2013, p.186) it is decided to use the tau² from metareg in the random effects case and then perform manually weighted regression based on the -reg- command to ensure comparabilty and ease of understanding for readers not familiar with the other commands. Additionally, 'reg' allows for cluster-reobust standard errors while metareg  doesn't. However, this leads to lenghty code lines in some cases.
*/

*-------------------------------------------------------------------------------
* Model selection baseline specificaiton (full observations)
*-------------------------------------------------------------------------------

* This do-file informs Table A7.

*-----------------------------------------------
* A: estimate a mixed effects / random effects meta-regression 
*-----------------------------------------------

*WLS-RE FAT-PET
quietly metareg elas elas_SE publish year_publish_c site_m NPL_n NPL_y NPL_u active_n active_u job_n job_u non_haz nuclear air water element_u North_America Asia Other_continent cleanup0 cleanup2 cleanup3 cleanup_unclear dist_greater_mean sample_c sale_ind num_expl_c oth_disamen oth_amen access industry demoecon time_control direction interaction log_log OLS_spatial, wsse(elas_SE) tau2test
scalar tau2 = e(tau2)
display tau2
gen elas_var_re_pb= elas_var + tau2
gen elas_SE_re_pb = sqrt(elas_var_re_pb)
gen precision_re_pb= 1/(elas_SE_re_pb)

foreach var in elas elas_SE elas_var publish year_publish_c site_m NPL_n NPL_y NPL_u active_n active_u job_n job_u non_haz nuclear air water element_u North_America Asia Other_continent cleanup0 cleanup2 cleanup3 cleanup_unclear dist_greater_mean sample_c sale_ind num_expl_c oth_disamen oth_amen access industry demoecon time_control direction interaction log_log OLS_spatial{
gen `var'2 = `var'/elas_SE_re_pb
}

*WLS-RE PEESE
quietly metareg elas elas_var publish year_publish_c site_m NPL_n NPL_y NPL_u active_n active_u job_n job_u non_haz nuclear air water element_u North_America Asia Other_continent cleanup0 cleanup2 cleanup3 cleanup_unclear dist_greater_mean sample_c sale_ind num_expl_c oth_disamen oth_amen access industry demoecon time_control direction interaction log_log OLS_spatial, wsse(elas_SE) tau2test

scalar tau2 = e(tau2)
display tau2
gen elas_var_re_pb_sq= elas_var + tau2
gen elas_SE_re_pb_sq = sqrt(elas_var_re_pb_sq)
gen precision_sq_re_pb_sq= 1/elas_var_re_pb_sq
gen precision_re_pb_sq= sqrt(precision_sq_re_pb_sq)

foreach var in elas elas_SE elas_var publish year_publish_c site_m NPL_n NPL_y NPL_u active_n active_u job_n job_u non_haz nuclear air water element_u North_America Asia Other_continent cleanup0 cleanup2 cleanup3 cleanup_unclear dist_greater_mean sample_c sale_ind num_expl_c oth_disamen oth_amen access industry demoecon time_control direction interaction log_log OLS_spatial{
gen `var'3 = `var'/elas_SE_re_pb_sq
}
 
*Q-test rejects homogeneity in all cases. --> RE generally preferred over FE, however publication bias is confirmed as well, which generally favours FE over RE.

*------------------------------------------------------------
* B: Test for unobserved study level effects via panel models
*------------------------------------------------------------
/*
The panel command "xtreg, re" for random effects panel models does not allow analytic weights ("aweight") or the estimation without a constant (this is needed in the manual WLS case). However, one of these methods would be necessary to perform WLS panel models with random effects similar to the baseline case without study level effects. The panel command "xtreg, mle" and the command "mixed" allow to use WLS and model study level effects simultaneously. Unfortunatly, the command "xttest0" to perform the Breusch-Pagan Lagrange Multiplier (BPLM) test for unobserved study level effects is only available after "xttest, re". In this situation it is decided to proceed as follows:
First, the commands "xtreg, mle" and "mixed" are used to estimate the desired WLS model with unobserved study level effects. "mixed" and "xtreg, mle" give identical coefficients and almost identical SE, but the residuals are different. (This can be regarded as a test for consistency) Then the "xtreg, re" alternative with a constant is estimated followed by the BPLM-test with "xttest0". As expected, regression results are very similar for "xtreg, mle nocons" and "xtreg, re". Hence, it is believed that, for the purpose of conducting the BPLM test, it is a viable approach to use the "xtreg, re" results.
*/

*WLS-RE FAT-PET
mixed elas2 precision_re_pb elas_SE2 publish2 year_publish_c2 site_m2 NPL_n2 NPL_y2 NPL_u2 active_n2 active_u2 job_n2 job_u2 non_haz2 nuclear2 air2 water2 element_u2 North_America2 Asia2 Other_continent2 cleanup02 cleanup22 cleanup32 cleanup_unclear2 dist_greater_mean2 sample_c2 sale_ind2 num_expl_c2 oth_disamen2 oth_amen2 access2 industry2 demoecon2 time_control2 direction2 interaction2 log_log2 OLS_spatial2, nocons ||ID_Study:

preserve
xtset ID_Study
xtreg elas2 precision_re_pb elas_SE2 publish2 year_publish_c2 site_m2 NPL_n2 NPL_y2 NPL_u2 active_n2 active_u2 job_n2 job_u2 non_haz2 nuclear2 air2 water2 element_u2 North_America2 Asia2 Other_continent2 cleanup02 cleanup22 cleanup32 cleanup_unclear2 dist_greater_mean2 sample_c2 sale_ind2 num_expl_c2 oth_disamen2 oth_amen2 access2 industry2 demoecon2 time_control2 direction2 interaction2 log_log2 OLS_spatial2, mle nocons

xtreg elas2 precision_re_pb elas_SE2 publish2 year_publish_c2 site_m2 NPL_n2 NPL_y2 NPL_u2 active_n2 active_u2 job_n2 job_u2 non_haz2 nuclear2 air2 water2 element_u2 North_America2 Asia2 Other_continent2 cleanup02 cleanup22 cleanup32 cleanup_unclear2 dist_greater_mean2 sample_c2 sale_ind2 num_expl_c2 oth_disamen2 oth_amen2 access2 industry2 demoecon2 time_control2 direction2 interaction2 log_log2 OLS_spatial2, re

xttest0 // 0.00 p=0.4781
restore

*WLS-RE PEESE
mixed elas3 elas_var3 precision_re_pb_sq publish3 year_publish_c3 site_m3 NPL_n3 NPL_y3 NPL_u3 active_n3 active_u3 job_n3 job_u3 non_haz3 nuclear3 air3 water3 element_u3 North_America3 Asia3 Other_continent3 cleanup03 cleanup23 cleanup33  cleanup_unclear3 dist_greater_mean3 sample_c3 sale_ind3 num_expl_c3 oth_disamen3 oth_amen3 access3 industry3 demoecon3 time_control3 direction3 interaction3 log_log3 OLS_spatial3, nocons ||ID_Study:

preserve
xtset ID_Study
xtreg elas3 elas_var3 precision_re_pb_sq publish3 year_publish_c3 site_m3 NPL_n3 NPL_y3 NPL_u3 active_n3 active_u3 job_n3 job_u3 non_haz3 nuclear3 air3 water3 element_u3 North_America3 Asia3 Other_continent3 cleanup03 cleanup23 cleanup33  cleanup_unclear3 dist_greater_mean3 sample_c3 sale_ind3 num_expl_c3 oth_disamen3 oth_amen3 access3 industry3 demoecon3 time_control3 direction3 interaction3 log_log3 OLS_spatial3, mle nocons

xtreg elas3 elas_var3 precision_re_pb_sq publish3 year_publish_c3 site_m3 NPL_n3 NPL_y3 NPL_u3 active_n3 active_u3 job_n3 job_u3 non_haz3 nuclear3 air3 water3 element_u3 North_America3 Asia3 Other_continent3 cleanup03 cleanup23 cleanup33  cleanup_unclear3 dist_greater_mean3 sample_c3 sale_ind3 num_expl_c3 oth_disamen3 oth_amen3 access3 industry3 demoecon3 time_control3 direction3 interaction3 log_log3 OLS_spatial3, re

xttest0 //0.04 p=0.4197
restore

/* Breusch-Pagan-LM test to identify unobserved study specific effects.
H0: no unobserved study specific effects
Results : 
BP: 0.00 , p=0.478
BP: 0.04 , p=0.420
--> fail to reject H0 (no unobserved study specific effects)
--> Pooled WLS without panel econometrics is appropriate. "WLS-RE PEESE" chosen as "preferred" model as publication bias is confirmed throughout.
*/



clear

